//
//  LoginViewController.m
//  B
//
//  Created by lanou on 16/5/12.
//  Copyright © 2016年 裴启航. All rights reserved.
//

#import "LoginViewController.h"
#import "RegistViewController.h"
#import "MeViewController.h"
#define kLogin @"http://app.dunkhome.com/users/sign_in.json"
@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *password;
@end

@implementation LoginViewController
- (void)viewWillDisappear:(BOOL)animated {
    MeViewController *me = [MeViewController new];
    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"nickName"];
    [me.userName setTitle:name forState:UIControlStateNormal];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self crateBack:@"icon_nav_cancel@2x.png"];
}
- (IBAction)login:(id)sender {
    if ([self isBlankString:_userName.text]) {
        [self.view popMessageWithTitle:@"用户名不能为空" postion:CENTER];
    } else {
        if ([self isBlankString:_password.text]) {
            [self.view popMessageWithTitle:@"密码不能为空" postion:CENTER];
        } else {
            NSString *login = [[NSUserDefaults standardUserDefaults] objectForKey:@"result"];
            if ([login isEqualToString:@"注销"]) {
                if ([_userName.text isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"nickName"]]) {
                    if ([_password.text isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"password"]]) {
                        [[NSUserDefaults standardUserDefaults] setObject:@"登录" forKey:@"result"];
                        [[NSUserDefaults standardUserDefaults] synchronize];
                        [self dismissViewControllerAnimated:YES completion:nil];
                    } else {
                        [self.view popMessageWithTitle:@"帐号或密码错误" postion:CENTER];
                    }
                } else {
                    [self loginRequest];
                }
            } else if ([login isEqualToString:@"登录"]) {
            
            } else {
                [self loginRequest];
            }
        }
    }
}
- (IBAction)regist:(id)sender {
    RegistViewController *registVc = [RegistViewController new];
    [self.navigationController pushViewController:registVc animated:YES];
}
- (void)loginRequest {
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    [session POST:kLogin parameters:@{@"user[email_or_name]":self.userName.text,@"user[password]":self.password.text} progress:^(NSProgress * _Nonnull uploadProgress) {
        NSLog(@"--uploadProgress%@", uploadProgress);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", responseObject);
        [[NSUserDefaults standardUserDefaults] setObject:responseObject[@"nick_name"] forKey:@"nickName"];
        [[NSUserDefaults standardUserDefaults] setObject:_password.text forKey:@"password"];
        [[NSUserDefaults standardUserDefaults] setObject:@"登录" forKey:@"result"];      [[NSUserDefaults standardUserDefaults] synchronize];
        [self dismissViewControllerAnimated:YES completion:nil];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
        // 判断当前网络
        [[AFNetworkReachabilityManager sharedManager] startMonitoring];
        [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            switch (status) {
                case AFNetworkReachabilityStatusNotReachable:
                    [self.view popMessageWithTitle:@"网络无连接" postion:CENTER];
                    break;
                case AFNetworkReachabilityStatusReachableViaWiFi:
                    NSLog(@"您正在使用WIFI网络");
                    [self.view popMessageWithTitle:@"帐号或密码错误" postion:CENTER];
                    break;
                case AFNetworkReachabilityStatusReachableViaWWAN:
                    NSLog(@"您正在使用2G/3G网络");
                    [self.view popMessageWithTitle:@"帐号或密码错误" postion:CENTER];
                    break;
                default:
                    break;
            }
        }];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
